package com.baidu.navigationdemo

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.FragmentContainerView
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.viewpager2.widget.ViewPager2
import com.baidu.navigationdemo.databinding.ActivityMainBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class MainActivity : AppCompatActivity() {
    private lateinit var binding:ActivityMainBinding
    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        /**
         底部是一个TabLayout
         顶部是一个ViewPager2
            如果界面有限且固定 + 横向滚动
            使用适配器模式

         TabLayout 标签栏的布局容器
         Tab       具体的每一个标签的类型
         */
        /*
        TabLayoutMediator(
            binding.tabLayout,
            binding.viewPager,
            object : TabLayoutMediator.TabConfigurationStrategy {
                @SuppressLint("UseCompatLoadingForDrawables")
                override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
                    //配置position对应的tab的显示内容
                    when(position){
                        0 -> {
                            tab.text = "Home"
                            tab.icon = resources.getDrawable(R.drawable.icon_home,null)
                        }
                        1 -> {
                            tab.text = "Detail"
                            tab.icon = resources.getDrawable(R.drawable.icon_videocam_,null)
                        }
                        2 -> {
                            tab.text = "More"
                            tab.icon = resources.getDrawable(R.drawable.icon_chrome_,null)
                        }
                    }
                }
            }
        )
        */

        //配置ViewPager2的显示内容
        binding.viewPager.adapter = NewsAdapter(
            listOf(HomeFragment(), DetailFragment(), MoreFragment()),
                supportFragmentManager,
                lifecycle
        )

        //配置TabLayout和ViewPager2的联动和显示
        /*
        TabLayoutMediator(binding.tabLayout, binding.viewPager,){ tab, position ->
            //配置position对应的tab的显示内容
            when(position){
                0 -> {
                    tab.text = "Home"
                    tab.icon = resources.getDrawable(R.drawable.icon_home,null)
                }
                1 -> {
                    tab.text = "Detail"
                    tab.icon = resources.getDrawable(R.drawable.icon_videocam_,null)
                }
                2 -> {
                    tab.text = "More"
                    tab.icon = resources.getDrawable(R.drawable.icon_chrome_,null)
                }
            }
        }.attach()
        */
        //如何手动添加标签

        val tab1 = binding.tabLayout.newTab()
        tab1.setText("Home")
        tab1.setIcon(resources.getDrawable(R.drawable.icon_home,null))
        binding.tabLayout.addTab(tab1,true) //默认选中第一个

       binding.tabLayout.addTab(binding.tabLayout.newTab().apply {
           text = "Detail"
           icon = resources.getDrawable(R.drawable.icon_videocam_,null)
       })
       binding.tabLayout.addTab(binding.tabLayout.newTab().apply {
           text = "More"
           icon = resources.getDrawable(R.drawable.icon_chrome_,null)
       })


       //监听TabLayout的选中事件
       binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
           override fun onTabSelected(tab: TabLayout.Tab?) {
               //让ViewPager2滚动到对应的页面
               binding.viewPager.setCurrentItem(tab!!.position,false)
           }
           override fun onTabUnselected(tab: TabLayout.Tab?) {

           }
           override fun onTabReselected(tab: TabLayout.Tab?) {

           }
       })
       //监听viewPager2的页面改变的事件
       binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
           override fun onPageSelected(position: Int) {
               //获取position对应Tab标签
               val tab = binding.tabLayout.getTabAt(position)
               //设置TabLayout选中对应的标签
               binding.tabLayout.selectTab(tab,true)
           }
       })

    }
}